package cn.melon.view.admin.library;

import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import cn.melon.mod.library.LibraryBooksInfo;
import cn.melon.mod.library.LibraryRequest;
import cn.melon.view.admin.library.ctr.LibraryPanelCtr;
import cn.melon.view.admin.library.ctr.LibraryTableMouse;

public class LibraryJPanel extends JPanel implements IUpdateBookTable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private LibraryTableMouse mouseListener = new LibraryTableMouse(this);
    private LibraryPanelCtr listener = new LibraryPanelCtr(this);
    
    LibraryRequest libraryRequest = new LibraryRequest(this);
    
    private JTextField Id = new JTextField(10);
    private JTextField name = new JTextField(10);
    private JButton queryButton;
    private JButton addButton;
    
    private String[] tableColName = {"书籍编号","书籍名称","出版社","作者","可借数量","借阅人学号","借出日期","应还日期"};
    private JTable table;
    DefaultTableModel tableModel;
    /**
     * Create the panel.
     */
    public LibraryJPanel() {
        initialize();
        libraryRequest.getAllBooks();
    }
    
    private void initialize() {
        this.setSize(1200, 1000);
        this.setBackground(Color.black);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel topPanel = new JPanel();
        topPanel.setSize(1200, 50);
        topPanel.setBackground(Color.darkGray);

        JPanel bookId = new JPanel();
        JLabel bookIdText = new JLabel("书籍编号:"); 
        bookId.add(bookIdText);
        bookId.add(Id);

        JPanel bookName = new JPanel();
        JLabel nameText = new JLabel("书籍名称:");
        bookName.add(nameText);
        bookName.add(name);

        queryButton = new JButton("查询");
        addButton = new JButton("书籍入库");

        topPanel.add(Box.createRigidArea(new Dimension(200, 15)));
        topPanel.add(bookId);
        topPanel.add(bookName);
        topPanel.add(queryButton);
        topPanel.add(Box.createRigidArea(new Dimension(400, 15)));
        topPanel.add(addButton);
        this.add(topPanel);

        JScrollPane tablePanel = new JScrollPane();
        tablePanel.setPreferredSize(new Dimension(360, 650));
        tableModel = new DefaultTableModel();
        table = new JTable(tableModel) {
            private static final long serialVersionUID = 1L;

            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        for(int i=0;i<tableColName.length;i++){
            tableModel.addColumn(tableColName[i]);
        }
        tablePanel.setViewportView(table);
        this.add(tablePanel);
        
        
        queryButton.addActionListener(listener);
        addButton.addActionListener(listener);
        table.addMouseListener(mouseListener);
    }

    public LibraryBooksInfo getTableRowBook(){
        int row = table.getSelectedRows()[0];
        LibraryBooksInfo booksInfo = new LibraryBooksInfo((String)tableModel.getValueAt(row, 1),(String)tableModel.getValueAt(row, 0),(String)tableModel.getValueAt(row, 2)
                ,(String)tableModel.getValueAt(row, 3),(String)tableModel.getValueAt(row, 4),(String)tableModel.getValueAt(row, 5),(String)tableModel.getValueAt(row, 6),(String)tableModel.getValueAt(row, 7),"");
        return booksInfo;
    }
    
    
    
    @Override
    public void updateTable(List<LibraryBooksInfo> books) {
        /*
         * 删除全部行
         */
        int rows = tableModel.getRowCount();
        for (int i = 0; i < rows; i++) {
            tableModel.removeRow(0);
        }
        
        /*
         * 添加信息
         */
        int bookNum = books.size();
        for(int i = 0 ; i < bookNum ; i ++){
            LibraryBooksInfo booksInfo = books.get(i);
            String[] book = {booksInfo.getBookID(),booksInfo.getBookName(),booksInfo.getPublisher(),
                    booksInfo.getAuthor(),booksInfo.getLoanable(),booksInfo.getBorrowStudent(),booksInfo.getBorrowDate(),booksInfo.getDDL()};
            tableModel.addRow(book);
        }
        
    }
}
